/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "queue.h"
#include "../../../Bsp/bsp_key.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for myTask02 */
osThreadId_t myTask02Handle;
const osThreadAttr_t myTask02_attributes = {
  .name = "myTask02",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for myQueue01 */
osMessageQueueId_t myQueue01Handle;
const osMessageQueueAttr_t myQueue01_attributes = {
  .name = "myQueue01"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void OutputTask(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of myQueue01 */
  myQueue01Handle = osMessageQueueNew (1, sizeof(uint8_t), &myQueue01_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of myTask02 */
  myTask02Handle = osThreadNew(OutputTask, NULL, &myTask02_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  KeyOperation key = {
    HAL_GPIO_ReadPin,
    HAL_GetTick,
    {
      GPIOA,
      GPIO_PIN_0,
      DOWN
    }
  };

  KeyStatus keyStatus = KEY_IDLE;
  /* Infinite loop */
  for(;;)
  {
    keyStatus = GetKeyStatus(&key);
    if (KEY_IDLE != keyStatus)
    {
      xQueueOverwrite(myQueue01Handle, &keyStatus);
    }
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_OutputTask */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_OutputTask */
void OutputTask(void *argument)
{
  /* USER CODE BEGIN OutputTask */
  uint8_t keyPressCount = 0;
# if 0
  UBaseType_t defaultTaskFreeStack = 0;
  UBaseType_t outputTaskFreeStack = 0;
# endif
  /* Infinite loop */
  for(;;)
  {
    xQueueReceive(myQueue01Handle, &keyPressCount, portMAX_DELAY);
    if (KEY_SHORT_PRESS == keyPressCount)
    {
      HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    }
    else if (KEY_DOUBLE_PRESS == keyPressCount)
    {
      HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
      for (uint8_t cnt = 0; cnt < 4; cnt++)
      {
        osDelay(200);
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
      }
    }
    else
    {
      HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
      for (uint8_t cnt = 0; cnt < 6; cnt++)
      {
        osDelay(200);
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
      }
    }
    
#   if 0
    defaultTaskFreeStack = uxTaskGetStackHighWaterMark(defaultTaskHandle);
    outputTaskFreeStack  = uxTaskGetStackHighWaterMark(myTask02Handle);
    printf("key press count : %d\r\n", keyPressCount);
    printf("key task free stack: %d \r\n", defaultTaskFreeStack);
    printf("output task free stack %d\r\n", outputTaskFreeStack);
#   endif
  }
  /* USER CODE END OutputTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

